<template>
	<!-- 排行榜列表页 -->
  <transition name="slide" >
    <music-list :rank="rank" :title="title" :bg-image="bgImage" :songs="songs"></music-list>
		<router-view></router-view>
  </transition>
</template>

<script >
  import MusicList from 'components/music-list/music-list'
  import {getMusicList} from 'api/rank'
  import {ERR_OK} from 'api/config'
  import {mapGetters,mapMutations} from 'vuex'
  import {createSong} from 'common/js/song'
	import axios from 'axios'
	import Vue from 'vue'
  export default {
    computed: {
      title() {
        return this.topList.topTitle
      },
      bgImage() {
        if (this.songs.length) {
          return this.songs[0].image
        }
        return ''
      },
      ...mapGetters([
        'topList'
      ])
    },
    data() {
      return {
        songs: [],
        rank: true
      }
    },
    created() {
      this._getMusicList()
			 
			console.log("songs",this.songs)
    },
    methods: {
      _getMusicList() {
        if (!this.topList.id) {
          this.$router.push('/rank')
          return
        }
        getMusicList(this.topList.id).then((res) => {
          if (res.code === ERR_OK) {//res.songlist 大于0
							this.songs = this._normalizeSongs(res.songlist)
          }
        })
      },
      _normalizeSongs(list) {
        let ret = []
        list.forEach((item) => {
          const musicData = item.data
          if (musicData.songid && musicData.albummid) {
            ret.push(createSong(musicData))
          }
        })
				ret.forEach((item) =>{//增加一个属性
					if(!item.pic){item.pic=item.image}
				})
        return ret
      },
			...mapMutations({
			    plen:'plength'
			})
    },
		watch: {
		  songs(newQuery) {
				 let sp=newQuery.length>0?newQuery.length:0	
				 let that=this;
				 setTimeout(()=>{
						if(!sp){that.$router.push('/rank')}
				 },1000)
				 this.plen(sp)
		    }
		},
    components: {
      MusicList,
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s ease

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>